﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;


using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;


namespace WP.RM.Form
{
    public partial class frmRMSelProductSemi : BaseQryRpt 
    {
        private string mstrProductFitemID = "";
        private string mstrProductName = "";
        public  bool HaveReturnValue = false;
        private DBAccess mdbcon = null;
        public frmRMSelProductSemi(DBAccess dbcon , string strCaption,string strProductFitemID)
        {
            InitializeComponent();
            this.Text = strCaption;
            mstrProductFitemID = strProductFitemID;
            mstrProductName = dbcon.GetValue("select productname from rmproduct where productfitemid='" + strProductFitemID + "'"); 
            mdbcon = dbcon;
            FillSemi();
        }

        private void FillSemi()
        {           
            tvSemi.Nodes.Clear();
            TreeNode tvNodeHdr = new TreeNode(mstrProductName);
            tvSemi.Nodes.Add(tvNodeHdr);
            FillSemiHdr(tvNodeHdr);
            this.StartPosition = FormStartPosition.CenterParent;
        }

        private void FillSemiHdr(TreeNode tvNode)
        {
            string strSQL = @"Select * FROM rmProductBOMSemi where  
                              ProductFItemID='{0}' and ParentSemiID =0 ";
            strSQL = string.Format(strSQL, mstrProductFitemID);
            DataTable dtSemiHdr = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drSemiHdr in dtSemiHdr.Rows)
            {
                TreeNode tvNodeHdr = new TreeNode(CvtFunc.ToString(drSemiHdr["SemiCode"]) + "-" + CvtFunc.ToString(drSemiHdr["SemiName"]));
                tvNodeHdr.Tag = CvtFunc.ToString(drSemiHdr["RecID"]);
                tvNode.Nodes.Add(tvNodeHdr);
                string strRecID = CvtFunc.ToString(tvNodeHdr.Tag);
                strSQL = @"with cte as
                            (
                                select RECID, SemiCode,SemiName,ParentSemiID,0 as lvl from rmProductBOMSemi
                                where recId = {0}
                                union all
                                select D.RECID,d.SemiCode,d.SemiName,D.ParentSemiID,lvl+1 from cte c inner join rmProductBOMSemi d
                                on c.recId = d.ParentSemiID
                            )
                            select * from cte where lvl>0";
                strSQL = string.Format(strSQL, strRecID);
                DataView dvSemi = mdbcon.GetDataTable(strSQL).DefaultView;
                FillSemiProduct(tvNodeHdr, dvSemi);
            }
        }

        private void FillSemiProduct(TreeNode tvNode, DataView dvSemi)
        {
            dvSemi.RowFilter = " ParentSemiID ='" + CvtFunc.ToString(tvNode.Tag) + "'";
            if (dvSemi.Count > 0)
            {
                foreach (DataRowView dgv in dvSemi)
                {
                    TreeNode tvNodeHdr = new TreeNode(CvtFunc.ToString(dgv["SemiCode"]) + "-" + CvtFunc.ToString(dgv["SemiName"]));
                    tvNodeHdr.Tag = CvtFunc.ToString(dgv["RecID"]);
                    tvNode.Nodes.Add(tvNodeHdr);
                    FillSemiProduct(tvNodeHdr, dvSemi);
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tvSemi.SelectedNode == null)
            {
                HaveReturnValue = false;
            }
            else
            {
                HaveReturnValue = true;
            }
            this.Hide();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            HaveReturnValue = false;
            this.Close(); 
        }

    }
}
